package com.ypinkj.app.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ypinkj.app.annotation.UserLoginToken;
import com.ypinkj.app.entity.UserApplyPosition;
import com.ypinkj.app.service.UserApplyPositionService;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultPage;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2020-10-16
 * <p>
 * <p>
 * 用于申请职位
 */
@RestController
@RequestMapping("/user-apply-position")
public class UserApplyPositionController {

    @Autowired
    private UserApplyPositionService userApplyPositionService;


    /**
     * 用户申请岗位接口
     *
     * @param userApplyPosition
     * @return
     */
    //@UserLoginToken
    @RequestMapping("ApplyPosition")
    public Result ApplyPosition(UserApplyPosition userApplyPosition) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("user_id", userApplyPosition.getUserId());
        queryWrapper.eq("position_id", userApplyPosition.getPositionId());
        queryWrapper.eq("company_id", userApplyPosition.getCompanyId());
        queryWrapper.eq("attachment_id", userApplyPosition.getAttachmentId());


        int count = userApplyPositionService.count(queryWrapper);
        if (count > 1) {
            return ResultUtil.error("已经申请过该岗位!");
        }


        boolean save = userApplyPositionService.save(userApplyPosition);

        if (!save) {
            ResultUtil.error("申请失败请重新申请!");
        }
        ;


        return ResultUtil.success(userApplyPosition);
    }


    /**
     * 判断该岗位是否可以申请
     *
     * @param userApplyPosition
     * @return
     */

    @RequestMapping("booleanPosition")
    public Result booleanPosition(UserApplyPosition userApplyPosition) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("user_id", userApplyPosition.getUserId());
        queryWrapper.eq("position_id", userApplyPosition.getPositionId());
        queryWrapper.eq("company_id", userApplyPosition.getCompanyId());
//        queryWrapper.eq("attachment_id",userApplyPosition.getAttachmentId());


        int count = userApplyPositionService.count(queryWrapper);
        if (count > 1) {
            return ResultUtil.error("已经申请过该岗位!");
        }


        return ResultUtil.success("可以申请该岗位");
    }


    /**
     * 查询那些人申请了 企业的PDF 文件
     *
     * @param
     * @return
     */
    //@UserLoginToken
    @RequestMapping("getApplyUser")
    public ResultPage getApplyUser(String companyId, Integer page, Integer limit) {

        List<UserApplyPosition> userApplyPositions = userApplyPositionService.talentCenter(companyId, page - 1, limit);


        Long conut = userApplyPositionService.talentCenterCount(companyId);

        /**
         * 获取所有的用户集合
         */
//        List<String> collect = list.stream().map(p -> p.getUserId()).collect(Collectors.toList());


        return ResultUtil.successCount(userApplyPositions,conut);
    }


}
